package April.april28;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/*
反射获取私有的构造方法运行
不推荐，破坏了程序的封装型，安全性
 */
public class ReflectDemo_2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class c = Class.forName("April.april28.Person");
//        Constructor[] getDeclaredConstructors() 获取所有的构造方法，包括私有的
        Constructor[] cons = c.getDeclaredConstructors();
        for (Constructor con : cons){
            System.out.println(con);
        }

//        Constructor getDeclaredConstructor(Class...c)获取到指定参数列表的构造方法
        Constructor con = c.getDeclaredConstructor(String.class,String.class);

//        Constructor类，父类AccessibleObject,定义方法setAccessible(boolean b)
        con.setAccessible(true);

        Object obj = con.newInstance("李四","18");
        System.out.println(obj);
    }
}
